package com.wits.platform.common.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.multipart.MultipartFile;

import com.wits.qso.entity.Attachment;

public class FileUtil {

	/**
	 * 
	 * @param file
	 * @param path 文件路径
	 * @param fileName 文件名
	 * @throws IOException
	 */
	public static void upload(MultipartFile file,String path,String fileName) throws IOException{
		InputStream in=null;
		FileOutputStream fs=null;
		try {
			File f=new File(path);
			if(!f.exists()){
				f.mkdirs();
			}
			in = file.getInputStream();
			fs = new FileOutputStream(path+File.separator+fileName);
			byte[] buffer = new byte[1024 * 1024];
			int byteread = 0;
			while ((byteread = in.read(buffer)) != -1) {
				fs.write(buffer, 0, byteread);
				fs.flush();
			}
		} catch (IOException e) {
			throw e;
		}finally{
			if(fs!=null){
				try {
					fs.close();
				} catch (IOException e) {
				}
			}
			if(in!=null){
				try {
					in.close();
				} catch (IOException e) {
				}
			}
			
		}
		


	}

	/**
	 * 
	 * @param response
	 * @param basePath 工程根路径
	 * @param contentType
	 * @param att
	 */
	public static void download(HttpServletResponse response, String basePath,String contentType,Attachment att){
		response.setContentType("textml;charset=UTF-8");
		//request.setCharacterEncoding("UTF-8");
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		
		String downLoadPath = basePath + File.separator+att.getFilePath();

		String realName=att.getOriginalName();
		long fileLength = new File(downLoadPath).length();

		response.setContentType(contentType);
		try {// new String(realName.getBytes("utf-8"), "ISO8859-1")
			response.setHeader("Content-disposition", "attachment; filename="+URLEncoder.encode(realName,"UTF-8"));
			response.setHeader("Content-Length", String.valueOf(fileLength));
			bis = new BufferedInputStream(new FileInputStream(downLoadPath));
			bos = new BufferedOutputStream(response.getOutputStream());
			byte[] buff = new byte[2048];
			int bytesRead;
			while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
				bos.write(buff, 0, bytesRead);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				bis.close();
			} catch (IOException e) {
			}
			try {
				bos.close();
			} catch (IOException e) {
			}
		}


	}
	
	/**
	 * 
	 * @param response
	 * @param basePath
	 *            工程根路径
	 * @param contentType
	 * @param att
	 */
	public static void download(HttpServletResponse response, String basePath,
			String contentType, Map<String, String> att) {
		response.setContentType("textml;charset=UTF-8");
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;

		String downLoadPath = basePath + File.separator + att.get("filePath");

		String realName = att.get("originalName");
		long fileLength = new File(downLoadPath).length();

		response.setContentType(contentType);
		try {
			response.setHeader("Content-disposition", "attachment; filename="
					+ URLEncoder.encode(realName,"UTF-8"));//new String(realName.getBytes("utf-8"), "ISO8859-1")
			response.setHeader("Content-Length", String.valueOf(fileLength));
			bis = new BufferedInputStream(new FileInputStream(downLoadPath));
			bos = new BufferedOutputStream(response.getOutputStream());
			byte[] buff = new byte[2048];
			int bytesRead;
			while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
				bos.write(buff, 0, bytesRead);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				bis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				bos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
